﻿using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Framework
{
    public static class StringHelper
    {
        public static byte[] Str2ByteArray(string txt)
        {
            if (string.IsNullOrEmpty(txt))
            {
                Logger.Log("StringHelper.Str2ByteArray, txt can not be null!");
                return new byte[] { };
            }
            return Encoding.UTF8.GetBytes(txt);
        }

        public static string ByteArray2Str(byte[] data)
        {
            if (null == data || data.Length == 0)
            {
                Logger.Log("StringHelper.ByteArray2Str, data can not be null!");
                return string.Empty;
            }
            return Encoding.UTF8.GetString(data);
        }

        public static string ByteArray2Base64Str(byte[] data)
        {
            if (null == data || data.Length == 0)
            {
                Logger.Log("StringHelper.ByteArray2Base64Str, data can not be null!");
                return string.Empty;
            }
            return System.Convert.ToBase64String(data);
        }

        public static byte[] Base64Str2ByteArray(string base64Str)
        {
            if (string.IsNullOrEmpty(base64Str))
            {
                Logger.Log("StringHelper.Base64Str2ByteArray, base64Str can not be null!");
                return new byte[] { };
            }
            return System.Convert.FromBase64String(base64Str);
        }

        //参考：https://docs.unity3d.com/2019.4/Documentation/Manual/BestPracticeUnderstandingPerformanceInUnity5.html
        public static bool CustomEndsWith(this string a, string b)
        {
            int ap = a.Length - 1;
            int bp = b.Length - 1;

            while (ap >= 0 && bp >= 0 && a[ap] == b[bp])
            {
                ap--;
                bp--;
            }

            return (bp < 0);
        }

        public static bool CustomStartsWith(this string a, string b)
        {
            int aLen = a.Length;
            int bLen = b.Length;

            int ap = 0; int bp = 0;

            while (ap < aLen && bp < bLen && a[ap] == b[bp])
            {
                ap++;
                bp++;
            }

            return (bp == bLen);
        }

        /// <summary>
        /// 字节数组转换到十六进制字符串
        /// </summary>
        /// <param name="buffer">字节数组</param>
        /// <returns></returns>
        public static string BufferToHexStr(byte[] buffer)
        {

            var strBuilder = new StringBuilder();
            foreach (var item in buffer)
            {
                strBuilder.Append(item.ToString("x2"));
            }
            return strBuilder.ToString();
        }

        /// <summary>
        /// 十六进制字符串转成字节数组
        /// </summary>
        /// <param name="hexStr">十六进制字符串</param>
        /// <returns></returns>
        public static byte[] HexStrToBuffer(string hexStr)
        {
            if (string.IsNullOrEmpty(hexStr) || hexStr.Length % 2 != 0)
            {
                Logger.Log("StringHelper.HexStrToBuffer, hexStr must be multiples of 2!");
                return null;
            }
            // Logger.Log("HexStrToBuffer hexStr = " + hexStr);
            byte[] buffer = new byte[hexStr.Length / 2];
            for (int idx = 0; idx < hexStr.Length; idx += 2)
            {
                string item = hexStr.Substring(idx, 2);
                // Logger.Log("item = " + item);
                buffer[idx / 2] = System.Convert.ToByte(item, 16);
            }
            return buffer;
        }
    }
}
